Working with R – RStudio

RStudio

Easier working with R

  • Syntax highlighting, code completion, and smart indentation
  • Easily manage multiple working directories and projects

More information

  • Workspace browser and data viewer
  • Plot history, zooming, and flexible image and PDF export
  • Integrated R help and documentation
  • Searchable command history

RStudio

Working with R in R Studio - 2 major panes:

  1. The R Console: “interprets” whatever you type
    • Calculator
    • Creating variables
    • Applying functions
  2. The Source/Editor: “Analysis” Script + Interactive Exploration
    • Static copy of what you did (reproducibility)
    • Try things out interactively, then add to your script

R Console

  • Where code is executed (where things happen)
  • You can type here for things interactively
  • Code is not saved on your disk

Source Editor

  • Where files open to
  • Have R code and comments in them
  • Can highlight and press (CMD+Enter (Mac) or Ctrl+Enter (Windows)) to run the code

In a .R file (we call a script), code is saved on your disk

Getting the editor

RStudio

R essentially is a command line with a set of functions loaded

R Uses Functions, in Packages

  • R revolves around functions
    • Commands that take input, performs computations, and returns results
  • Functions are enclosed in packages
    • When you download R, it has a “base” set of functions/packages (base R)
    • You can install additional packages for your uses from CRAN or GitHub
    • These additional packages are written by RStudio or R users/developers (like us)
    • Think of them as “R Extensions”

Picture of R package stickers

Using Packages

  • You need to be familiar with base R - answers on Google commonly use it
  • We will focus on newer and more intuitive ways to do things (tidyverse), not in base R
  • RStudio (the company) makes a lot of great packages
  • Not all packages available on CRAN or GitHub are trustworthy
  • Who wrote it? Hadley Wickham is a major authority on R (Employee and Developer at RStudio)
  • How to trust an R package: http://simplystatistics.org/2015/11/06/how-i-decide-when-to-trust-an-r-package/

Picture of Hadley Wickham (source: https://twitter.com/hadleywickham)

Let’s take a look at R Studio ourselves!

RStudio

Let’s start by making an RStudio “Project”.

  1. Helps you organize your work.
  2. Helps with working directories (discussed later).
  3. Allows you to easily know which project you’re on.

Go to File → New Project → New Directory → New Project

Call your Project “Intro_to_R”

R Markdown file

R Markdown files (.Rmd) help generate reports that include your code and output. Think of them as fancier scripts.

  1. Helps you describe your code
  2. Allows you to check the output
  3. Can create many different file types

Code chunks

Within R Markdown files are code “chunks”

This is where you can type R code and run it!

Knit

knit

Create an R Markdown file

Go to File → New File → R Markdown

Call your file “first_markdown”

RStudio layout

RStudio layout

RStudio Layout

If RStudio doesn’t look the way you want (or like our RStudio), then do:

RStudio –> Preferences –> Pane Layout

Workspace/Environment

Workspace/Environment

  • Tells you what objects are in R
  • What exists in memory/what is loaded?/what did I read in?

History

  • Shows previous commands. Good to look at for debugging, but don’t rely on it as a script. Make a script!
  • Also type the “up” key in the Console to scroll through previous commands

Other Panes

  • Files - shows the files on your computer of the directory you are working in
  • Viewer - can view data or R objects
  • Help - shows help of R commands
  • Plots - pretty pictures and figures
  • Packages - list of R packages that are loaded in memory

Useful R Studio Shortcuts

Lab: Starting with R and RMarkdown

Using the Viewer

The View command allows you to view data in a spreadsheet format. Run the following command:

View(mtcars)

Website